<div class="alert alert-info alert-dismissible fade show" role="alert">
    <button type="button" class="close" data-dismiss="alert" aria-label="Close">
        <span aria-hidden="true">&times;</span>
    </button>
    你可以在此检查文件修改情况并恢复初始文件
</div>
<div class="card">
    <div class="card-block">
        <h4 class="card-title">文件防篡改开关
            <div class="pull-right">
                <input type="checkbox" name="tamperproof_onoff" data-on-text="开" data-off-text="关">
            </div>
        </h4>
    </div>
    <div class="card-block" id="tamperproof_box">
        <hr>
        <h5>保护状态</h5>
        <p>当前已保护文件：<span class="text-danger">{total_files}</span></p>
        <div class="card-block">
            <button type="button" class="btn btn-primary" id="scan_protected_files">扫描文件改动</button>
        </div>
        <div class="card-block" id="scan_result"></div>
        <hr>
        <h5>操作文件</h5>
        <div class="card-block">
            <div class="row">
                <div class="col-4">
                    <button type="button" class="btn btn-warning" id="protect_current_files">保护当前文件</button>
                </div>
                <div class="col-6">
                    <div class="progress">
                        <div class="progress-bar progress-bar-striped progress-bar-animated" id="protect_progress" role="progressbar" aria-valuenow="0" aria-valuemin="0" aria-valuemax="100" style="width: 0%"></div>
                    </div>
                </div>
            </div>
        </div>
        <hr>
        <h5>设置排除目录或文件</h5>
        <div class="form-group">
            <label for="textarea_exclude">要排除的目录或文件，每行一个，以网站根目录为准，最前不要带 / 符号，如果是目录以 / 结尾</label>
            <textarea class="form-control" id="textarea_exclude" rows="8">{exclude}</textarea>
        </div>
        <button type="button" class="btn btn-primary" id="protect_exclude_files">保存</button>
    </div>
</div>
<!-- Modal -->
<div class="modal fade" id="confirmModal" tabindex="-1" role="dialog" aria-labelledby="confirmModalLabel" aria-hidden="true">
    <div class="modal-dialog" role="document">
        <div class="modal-content">
            <div class="modal-header">
                <h5 class="modal-title" id="confirmModalLabel">文件保护警告</h5>
                <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                    <span aria-hidden="true">&times;</span>
                </button>
            </div>
            <div class="modal-body">
                将当前的文件设置为保护状态，并覆盖以前版本的设置，需要较长的时间（视本地文件数量而定）
                <br /><br /><b class="text-danger">此操作无法取消，确定要操作吗？</b>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-secondary" data-dismiss="modal">取消</button>
                <button type="button" class="btn btn-danger" id="btn_protect_begin">开始保护</button>
            </div>
        </div>
    </div>
</div>
<div class="modal fade" id="historyModal" tabindex="-1" role="dialog" aria-labelledby="historyModal" aria-hidden="true">
    <div class="modal-dialog" role="document">
        <div class="modal-content">
            <div class="modal-header">
                <h5 class="modal-title" id="historyModalLabel">文件保护历史</h5>
                <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                    <span aria-hidden="true">&times;</span>
                </button>
            </div>
            <div class="modal-body">Loading</div>
            <div class="modal-footer">
                <button type="button" class="btn btn-secondary" data-dismiss="modal">关闭</button>
                <button type="button" class="btn btn-danger" id="btn_history_restore">开始恢复</button>
            </div>
        </div>
    </div>
</div>
<script type="text/javascript">
    $(document).ready(function () {
        $("input[name='tamperproof_onoff']").bootstrapSwitch({
            state: {enable},
            onColor: "success",
            offColor: "danger",
            onSwitchChange: function (event, state) {
                if(state) {
                    $("#tamperproof_box").show(200);
                } else {
                    $("#tamperproof_box").hide(200);
                }
                $.post("?m=tamperproof&a=option_onoff", {state: state ? 1 : 0}, function (data) {
                    if(data.status == "error") {
                        alert(data.msg);
                    }
                }, "json");
            }
        });
        if(!{enable}) {
            $("#tamperproof_box").hide();
        }

        $("#scan_protected_files").bind("click", function () {
            scan_protected_files();
        });

        $("#protect_exclude_files").bind("click", function () {
            protect_exclude_files();
        });

        $("#btn_history_restore").bind("click", function () {
            handle_history_restore();
        });

        $("#protect_current_files").bind("click", function () {
            $("#confirmModal").modal({focus: true, keyboard: true, backdrop: true});
        });

        $("#btn_protect_begin").bind("click", function () {
            $('#confirmModal').modal('hide')
            $("#protect_current_files").attr("disabled", true);
            $("#protect_current_files").text("扫描本地文件");
            $("#protect_progress").css("width", "5%");
            scan_local_files();
        });

    });




    // 扫描文件改动
    function scan_protected_files() {
        $("#scan_protected_files").attr("disabled", true);
        $("#scan_protected_files").text("正在扫描 ...");
        $("#scan_result").show(200);
        $("#scan_result").html("<i class='fa fa-refresh fa-spin'>");
        $.ajax({
            url: "?m=tamperproof&a=scan_protected_files",
            type: "POST",
            data: {code: "{tamperproof_verify_code}"},
            dataType: "json",
            error: function(xhr,textStatus){
                alert(textStatus);
                reset_protected_files();
            },
            success: function (data,textStatus,jqXHR) {
                if(data.status == "error") {
                    alert(data.msg);
                    reset_protected_files();
                } else {
                    $("#scan_result").html(data.msg);
                    $("#scan_protected_files").attr("disabled", false);
                    $("#scan_protected_files").text("扫描文件改动");
                }
            }
        });
    }

    function reset_protected_files() {
        $("#scan_result").hide(200);
        $("#scan_result").html("");
        $("#scan_protected_files").attr("disabled", false);
        $("#scan_protected_files").text("扫描文件改动");
    }

    function reset_protect_status() {
        $("#protect_current_files").attr("disabled", false);
        $("#protect_current_files").text("保护当前文件");
        $("#protect_progress").css("width", "0%");
    }
    // 第一步扫描本地文件
    function scan_local_files() {
        $.ajax({
            url: "?m=tamperproof&a=scan_local_files",
            type: "POST",
            data: {code: "{tamperproof_verify_code}"},
            dataType: "json",
            error: function(xhr,textStatus){
                alert(textStatus);
                reset_protect_status();
            },
            success: function (data,textStatus,jqXHR) {
                if(data.status == "error") {
                    alert(data.msg);
                    reset_protect_status();
                } else {
                    handle_protect_file(data.msg, 0);
                }
            }
        });
    }
    // 第二步，循环处理文件
    function handle_protect_file(files, index) {
        var total = files.length;
        var t = parseFloat(95 / total);
        var c = 5 + index * t;

        if(index == total) {
            $("#protect_current_files").text("保护文件完成");
            $("#protect_current_files").removeClass("btn-warning");
            $("#protect_current_files").addClass("btn-success");
            $("#protect_progress").css("width", "100%");
            return
        }

        $("#protect_current_files").text((index+1) + "/" + total);
        $.ajax({
            url: "?m=tamperproof&a=protect_local_file",
            type: "POST",
            data: {code: "{tamperproof_verify_code}", "location": files[index]},
            dataType: "json",
            async: false,
            error: function(xhr,textStatus){
                alert(textStatus);
                reset_protect_status();
            },
            success: function (data,textStatus,jqXHR) {
                if(data.status == "error") {
                    alert(data.msg);
                    reset_protect_status();
                } else {
                    $("#protect_progress").css("width", c + "%");

                    setTimeout(function () {
                        handle_protect_file(files, index+1);
                    }, 100);
                }
            }
        });
    }

    // 排除文件或目录
    function protect_exclude_files() {
        $.ajax({
            url: "?m=tamperproof&a=protect_exclude_files",
            type: "POST",
            data: {code: "{tamperproof_verify_code}", "exclude": $("#textarea_exclude").val() },
            dataType: "json",
            async: false,
            error: function(xhr,textStatus){
                alert(textStatus);
            },
            success: function (data,textStatus,jqXHR) {
                alert(data.msg);
            }
        });
    }

    // 显示文件更改历史记录
    function show_protect_history(location) {
        $.ajax({
            url: "?m=tamperproof&a=protect_history",
            type: "POST",
            data: {code: "{tamperproof_verify_code}", "location": location },
            dataType: "json",
            async: false,
            error: function(xhr,textStatus){
                alert(textStatus);
            },
            success: function (data,textStatus,jqXHR) {
                if(data.status == "error") {
                    alert(data.msg);
                } else {
                    $("#historyModal .modal-body").html(data.msg);
                    $("#historyModal").modal({focus: true, keyboard: true, backdrop: true});
                }
            }
        });
    }

    function handle_history_restore() {
        if($("#historyModal input[type='radio']:checked").length == 0) {
            alert("请选择要恢复项目！");
            return;
        }
        var id = $("#historyModal input[type='radio']:checked").attr("data-id");

        $.ajax({
            url: "?m=tamperproof&a=protect_restore_file",
            type: "POST",
            data: {code: "{tamperproof_verify_code}", "id": id },
            dataType: "json",
            async: false,
            error: function(xhr,textStatus){
                alert(textStatus);
            },
            success: function (data,textStatus,jqXHR) {
                alert(data.msg);
                $('#historyModal').modal('hide')
            }
        });
    }


</script>